package com.boy.rpc.framework.console.service.impl;

import com.boy.rpc.framework.console.Constant;
import com.boy.rpc.framework.console.workbench.WorkbenchCodeEnum;
import com.boy.rpc.framework.console.dao.RedisDao;
import com.boy.rpc.framework.console.service.IRedisService;
import com.boy.rpc.framework.console.utils.InitRedisConnection;
import com.boy.rpc.framework.console.workbench.WorkbenchResult;
import com.boy.rpc.framework.console.workbench.constant.WebConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisConnectionUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


/**
 * Created by wesley on 2017-03-16.
 */
@Service
public class RedisServiceImpl implements IRedisService{

    private static final Logger logger = LoggerFactory.getLogger(RedisServiceImpl.class);

    @Autowired
    private RedisDao redisDao;

    /**
     * 获取key-value操作
     * @param serverName
     * @param dbIndex
     * @param dataType
     * @param key
     * @return
     */
    @Override
    public WorkbenchResult getKV(String serverName, int dbIndex, String dataType, String key) {
        logger.debug("view getKV operations start！！");
        Object values = null;
        //服务注册的时候，已经VALUE默认设置为String类型的
        switch(dataType) {
            case "STRING":
                values = redisDao.getKV(serverName, dbIndex, key);
                break;
            case "NONE":
                //if showType = ShowTypeEnum.hide
                dataType = getDataType(serverName, dbIndex, key);
                values = getKV(serverName, dbIndex, key);
                break;
        }

        final String dataType1 = dataType;
        final Object values1 = values;
        WorkbenchResult workbenchResult = WorkbenchResult.custom().setOK(WorkbenchCodeEnum.valueOf(WebConstant.OK_REDIS_KV_UPDATE), new Object() {
            public String dataType;
            public Object values;
            {
                dataType = dataType1;
                values = values1;
            }
        }).build();
        logger.debug("getKV return workbenchResult"+workbenchResult);
        return workbenchResult;
    }

    /**
     * 获取redis-key的类型
     * @param serverName
     * @param dbIndex
     * @param key
     * @return
     */
    private String getDataType(String serverName, int dbIndex, String key) {
        RedisTemplate redisTemplate = Constant.redisTemplatesMap.get(serverName);
        RedisConnection connection = RedisConnectionUtils.getConnection(redisTemplate.getConnectionFactory());
        connection.select(dbIndex);
        DataType dataType = connection.type(key.getBytes());
        connection.close();
        return dataType.name().toUpperCase();
    }

    /**
     * 获取key-values值
     * @param serverName
     * @param dbIndex
     * @param key
     * @return
     */
    private Object getKV(String serverName, int dbIndex, String key) {
        RedisTemplate redisTemplate = Constant.redisTemplatesMap.get(serverName);
        RedisConnection connection = RedisConnectionUtils.getConnection(redisTemplate.getConnectionFactory());
        connection.select(dbIndex);
        DataType dataType = connection.type(key.getBytes());
        connection.close();
        Object values = null;
        switch(dataType) {
            case STRING:
                values = redisDao.getKV(serverName, dbIndex, key);
                break;
            case NONE:
                //never be here
                values = null;
        }
        return values;
    }

    /**
     * 添加key-value操作
     * @param serverName
     * @param dbIndex
     * @param dataType
     * @param key
     * @param values
     * @param scores
     * @param members
     * @param fields
     */
    @Override
    public void addKV(String serverName, int dbIndex, String dataType,
                      String key,
                      String[] values, double[] scores, String[] members, String[] fields) {
        logger.debug("redisService addKV start-->param: dataType-->"+dataType+",key-->"+key+",value-->"+values);
        switch(dataType) {
            case "STRING":
                redisDao.addKV(serverName, dbIndex, key, values);
                break;
        }
    }

    @Override
    public void delKV(String serverName, int dbIndex, String deleteKeys) {
        logger.debug("redisService delKv key param:"+deleteKeys);
        redisDao.delKeys(serverName,dbIndex,deleteKeys);
    }

    @Override
    public void addRedisServer(String name, String host, int port, String password) {
        InitRedisConnection.createRedisConnection(name,host,port,password,null);
    }

    @Override
    public void delValue(String serverName, int dbIndex, String key) {
        redisDao.delValue(serverName, dbIndex, key);
    }

    @Override
    public void updateValue(String serverName, int dbIndex, String key, String value) {
        redisDao.updateValue(serverName, dbIndex, key, value);
    }
}
